﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SoftwareMonkeys.LogClimber.Entities;
using System.Xml;

namespace SoftwareMonkeys.LogClimber.Tests
{
    static public class LogTestUtilities
    {
        static public Log CreateMockLog(int totalChildEntries, int totalSubChildEntries)
        {
            Log log = new Log();

            log.ChildEntries = AddMockEntries(log.ChildEntries, totalChildEntries);

            foreach (Entry entry in log.ChildEntries)
            {
                entry.ChildEntries = AddMockEntries(entry.ChildEntries, totalSubChildEntries);
            }

            return log;
        }

        static public Entry[] AddMockEntries(Entry[] entries, int numberOfEntries)
        {
            List<Entry> list = new List<Entry>(entries);

            for (int i = 0; i < numberOfEntries; i++)
            {
                Entry child = new Entry();

                child.Data = "Data #" + i;
                child.ID = Guid.NewGuid();
                child.Component = "Component #" + i;
                child.Method = "Method #" + i;

                list.Add(child);
            }

            return list.ToArray();
        }


        static public Entry CreateMockEntry(Log log, string data, Guid parentID)
        {
            Entry entry = new Entry();
            entry.Log = log;
            entry.Data = data;
            entry.ParentID = parentID;
            entry.ID = Guid.NewGuid();

            return entry;
        }

        static public string CreateBrokenMockLinearLog()
        {
        
            string xml = @"<?xml version='1.0'?>
            <?xml-stylesheet type='text/xsl' href='../../../LogTemplate.xsl'?>
            <Log>
            <Entry>
            <ID>f88adf19-c526-405d-98ca-49865d4dc863</ID>
            <ParentID>00000000-0000-0000-0000-000000000000</ParentID>
            <Indent>0</Indent>
            <LogLevel>Debug</LogLevel>
            <Timestamp>14/01/2011 9:26:32 AM</Timestamp>
            <Component>ASP.global_asax</Component>
            <Method>Application_BeginRequest</Method>
            <Data>Beginning application request.</Data>
            </Entry>


            <Entry>
            <ID>b9e341ab-bcff-4592-9dd0-31a542ee08d0</ID>
            <ParentID>00000000-0000-0000-0000-000000000000</ParentID>
            <Indent>0</Indent>
            <LogLevel>Debug</LogLevel>
            <Timestamp>14/01/2011 9:26:35 AM</Timestamp>
            <Component>ASP.admin_tests_testlogging_aspx</Component>
            <Method>Page_Load</Method>
            <Data>Testing logging</Data>
            </Entry>


            <Entry>
            <ID>a089f01a-bdb3-47f1-bb0a-4aa5b9f6f84f</ID>
            <ParentID>b9e341ab-bcff-4592-9dd0-31a542ee08d0</ParentID>
            <Indent>1</Indent>
            <LogLevel>Debug</LogLevel>
            <Timestamp>14/01/2011 9:26:35 AM</Timestamp>
            <Component>ASP.admin_tests_testlogging_aspx</Component>
            <Method>Page_Load</Method>
            <Data>Test debug message</Data>
            </Entry>


            <Entry>
            <ID>df15c38f-9f26-4345-b0ea-221c9a04052c</ID>
            <ParentID>b9e341ab-bcff-4592-9dd0-31a542ee08d0</ParentID>
            <Indent>1</Indent>
            <LogLevel>Debug</LogLevel>
            <Timestamp>14/01/2011 9:26:35 AM</Timestamp>
            <Component>ASP.admin_tests_testlogging_aspx</Component>
            <Method>DoSomething</Method>
            <Data>Doing something</Data>
            </Entry>

			<Log>

            <Entry>
            <ID>a4e57f29-b9fc-4cbb-aa82-11a0b0385b6c</ID>
            <ParentID>df15c38f-9f26-4345-b0ea-221c9a04052c</ParentID>
            <Indent>2</Indent>
            <LogLevel>Debug</LogLevel>
            <Timestamp>14/01/2011 9:26:35 AM</Timestamp>
            <Component>ASP.admin_tests_testlogging_aspx</Component>
            <Method>DoSomething</Method>
            <Data>The &apos;Doing something&apos; group should be a sub group of the &apos;Testing logging&apos; group.</Data>
            </Entry>

            <Entry>
            <ID>1E883E26-CF39-43d1-B5E3-1685DF354B68</ID>
            <ParentID>00000000-0000-0000-0000-000000000000</ParentID>
            <Indent>0</Indent>
            <LogLevel>Debug</LogLevel>
            <Timestamp>14/01/2011 9:26:35 AM</Timestamp>
            <Component>SomeComponent</Component>
            <Method>SomeMethod</Method>
            <Data>Another root item</Data>
            </Entry>

            <Entry>
            <ID>4B0AE2A8-102E-4bbb-A885-5F2E6F665160</ID>
            <ParentID>1E883E26-CF39-43d1-B5E3-1685DF354B68</ParentID>
            <Indent>1</Indent>
            <LogLevel>Debug</LogLevel>
            <Timestamp>14/01/2011 9:26:36 AM</Timestamp>
            <Component>SomeComponent</Component>
            <Method>SomeSubMethod</Method>
            <Data>Doing something within a group</Data>
            </Entry>

            </Log>
            ";
            
            return xml;
        }
        
        static public XmlDocument CreateMockLinearLogDoc()
        {
            string xml = @"<?xml version='1.0'?>
            <Log>
            <Entry>
            <ID>f88adf19-c526-405d-98ca-49865d4dc863</ID>
            <ParentID>00000000-0000-0000-0000-000000000000</ParentID>
            <Indent>0</Indent>
            <LogLevel>Debug</LogLevel>
            <Timestamp>14/01/2011 9:26:32 AM</Timestamp>
            <Component>ASP.global_asax</Component>
            <Method>Application_BeginRequest</Method>
            <Data>Beginning application request.</Data>
            </Entry>


            <Entry>
            <ID>b9e341ab-bcff-4592-9dd0-31a542ee08d0</ID>
            <ParentID>00000000-0000-0000-0000-000000000000</ParentID>
            <Indent>0</Indent>
            <LogLevel>Debug</LogLevel>
            <Timestamp>14/01/2011 9:26:35 AM</Timestamp>
            <Component>ASP.admin_tests_testlogging_aspx</Component>
            <Method>Page_Load</Method>
            <Data>Testing logging</Data>
            </Entry>


            <Entry>
            <ID>a089f01a-bdb3-47f1-bb0a-4aa5b9f6f84f</ID>
            <ParentID>b9e341ab-bcff-4592-9dd0-31a542ee08d0</ParentID>
            <Indent>1</Indent>
            <LogLevel>Debug</LogLevel>
            <Timestamp>14/01/2011 9:26:35 AM</Timestamp>
            <Component>ASP.admin_tests_testlogging_aspx</Component>
            <Method>Page_Load</Method>
            <Data>Test debug message</Data>
            </Entry>


            <Entry>
            <ID>df15c38f-9f26-4345-b0ea-221c9a04052c</ID>
            <ParentID>b9e341ab-bcff-4592-9dd0-31a542ee08d0</ParentID>
            <Indent>1</Indent>
            <LogLevel>Debug</LogLevel>
            <Timestamp>14/01/2011 9:26:35 AM</Timestamp>
            <Component>ASP.admin_tests_testlogging_aspx</Component>
            <Method>DoSomething</Method>
            <Data>Doing something</Data>
            </Entry>


            <Entry>
            <ID>a4e57f29-b9fc-4cbb-aa82-11a0b0385b6c</ID>
            <ParentID>df15c38f-9f26-4345-b0ea-221c9a04052c</ParentID>
            <Indent>2</Indent>
            <LogLevel>Debug</LogLevel>
            <Timestamp>14/01/2011 9:26:35 AM</Timestamp>
            <Component>ASP.admin_tests_testlogging_aspx</Component>
            <Method>DoSomething</Method>
            <Data>The &apos;Doing something&apos; group should be a sub group of the &apos;Testing logging&apos; group.</Data>
            </Entry>

            <Entry>
            <ID>1E883E26-CF39-43d1-B5E3-1685DF354B68</ID>
            <ParentID>00000000-0000-0000-0000-000000000000</ParentID>
            <Indent>0</Indent>
            <LogLevel>Debug</LogLevel>
            <Timestamp>14/01/2011 9:26:35 AM</Timestamp>
            <Component>SomeComponent</Component>
            <Method>SomeMethod</Method>
            <Data>Another root item</Data>
            </Entry>

            <Entry>
            <ID>4B0AE2A8-102E-4bbb-A885-5F2E6F665160</ID>
            <ParentID>1E883E26-CF39-43d1-B5E3-1685DF354B68</ParentID>
            <Indent>1</Indent>
            <LogLevel>Debug</LogLevel>
            <Timestamp>14/01/2011 9:26:36 AM</Timestamp>
            <Component>SomeComponent</Component>
            <Method>SomeSubMethod</Method>
            <Data>Doing something within a group</Data>
            </Entry>

            </Log>
            ";

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            return doc;
        }
    }
}
